package entity.dao.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import util.Parameters;
import util.Util;

import entity.Project;
import entity.dao.ProjectDAO;

public class MysqlProjectDAO extends ProjectDAO {

	Connection connection;
	Statement st;
	ResultSet rs;

	public MysqlProjectDAO() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://"
					+ Parameters.MYSQL_SERVER + "/" + Parameters.MYSQL_DB,
					Parameters.MYSQL_USER, Parameters.MYSQL_PASSWORD);
			st = connection.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e2) {
			e2.printStackTrace();
		}
	}

	@Override
	public boolean saveData(String guiDescriptor, String dataProvider,
			String projectName) {

		try {
			st.executeUpdate("INSERT INTO projects ( guiDescriptor, dataProvider) VALUES ( '"
					+ guiDescriptor
					+ "', '"
					+ dataProvider
					+ "') WHERE projectName = '" + projectName + "' ");

			st.close();
			connection.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	@Override
	public boolean saveDataAs(Project project) {
		
		try{
			st.executeUpdate("INSERT INTO projects (guiDescriptor, dataProvider, projectName) VALUES ( '"
					+ project.getGuiDescriptor().toString()
					+ "', '"
					+ project.getDataProvider().toString()
					+ "', '"
					+ project.getProjectName() + "')");
			
			st.close();
			connection.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return true;
	}

	@Override
	public Project load(int idproject) {

		Project p = null;
		
		try{
			rs = st.executeQuery("SELECT * FROM projects WHERE idprojects = '"+idproject+"' ");
			
			while(rs.next()){
				
				int id = Integer.valueOf(rs.getString("idprojects") );
				JSONArray guiDescriptor = new JSONArray(rs.getString("guiDescriptor"));
				JSONArray dataProvider = new JSONArray(rs.getString("dataProvider"));
				String projectName = rs.getString("projectName");
				
				p = new Project(id, guiDescriptor , dataProvider , projectName);
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}catch(JSONException e2){
			e2.printStackTrace();
		}
				
		return p;
	}

	@Override
	public Project[] loadAllProject() {

		Project[] projects = null;
		Project p = null;
		int i = 0;
		
		try{
			
			rs = st.executeQuery("SELECT * FROM projects");
			projects = new Project[Util.getResultSetRowsNumber(rs)];
			
			do{
				int id = Integer.valueOf(rs.getString("idprojects"));
				JSONArray guiDescriptor = new JSONArray(rs.getString("guiDescriptor"));
				JSONArray dataProvider = new JSONArray(rs.getString("dataProvider"));
				String projectName = rs.getString("projectName");
				
				p = new Project(id, guiDescriptor, dataProvider, projectName);
				projects[i] = p;
				
				i++;
			}while(rs.next());
						
		}catch(SQLException e){
			e.printStackTrace();
		}catch(JSONException e2){
			e2.printStackTrace();
		}
		
		
		return projects;
	}

}
